package com.client.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.client.bean.User;
import com.client.common.SpinnerProgressDialog;
import com.client.common.ValueStack;
import com.client.db.DataService;
import com.client.task.LoginlTask;
import com.clinet.cache.Log;

public class UserLoginAct extends BaseAct {

	private Context mContext = UserLoginAct.this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userloginact);
		init();
		getUser();
		setValue();
		onclick();
	}

	private DataService mDataService=null;
	private User bean=null;
	private void getUser() {
		Log.i("-----------getUser--------");
		mDataService=new DataService(mContext);
		if(mDataService.hasInitUser())
		{
			Log.i("-----------hasInitUser--------");
			bean=mDataService.getCurrUser();
			Log.i("-----------mUser--------"+bean);
		}else
		{
			Log.i("---new--------saveUser--------");
			bean=new User();
			bean.deviceName = SartAct.deviceName;
			bean.deviceNum = SartAct.devId;
			bean.deviceType =SartAct. deviceType;
			bean.current="true";
			Log.i("-----------mUser--------"+bean);
			mDataService.saveUser(bean);
		}
	}

	private void getValue() {
		bean.userName = username.getText().toString();
		bean.password = password.getText().toString();
		if (remember.isChecked()) {
			bean.setRemember(true);
		} else {
			bean.setRemember(false);
		}

		if (autologin.isChecked()) {
			bean.setAutologin(true);
		} else {
			bean.setAutologin(false);
		}
	}


	private void setValue() {
		if(username==null||bean==null) return ;
		username.setText(bean.userName);
		password.setText(bean.password);
		if (bean.isRemember()) {
			remember.setChecked(true);
		} else {
			remember.setChecked(false);
		}
		if (bean.isAutologin()) {
			autologin.setChecked(true);
		} else {
			autologin.setChecked(false);
		}
	}

	private void onclick() {
		remember.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (remember.isChecked()) {
					bean.setRemember(true);
				} else {
					bean.setRemember(false);
				}
			}
		});

		autologin.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (autologin.isChecked()) {
					bean.setAutologin(true);
				} else {
					bean.setAutologin(false);
				}
			}
		});
		login.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				SpinnerProgressDialog.showDialog(mContext,"正在获取数据,请稍后...");
				getValue();
				LoginlTask.Callback callback = new LoginlTask.Callback() {

					@Override
					public void onComplete(final Integer result) {
						SpinnerProgressDialog.dismissDialog();
						switch (result) {
						case 1:
							finish();
							Intent intent = new Intent(mContext,
									CollectionGridAct.class);
							bean.deviceNum=SartAct.devId;
							mDataService.updateUser(bean);
							Log.i("LoginlTask----------"+bean.toString());
							ValueStack.getInstance().setSessionValue("USER", bean);
							mContext.startActivity(intent);
							break;
						case 0:
							Toast.makeText(mContext, "用户名或密码错误!", Toast.LENGTH_LONG).show();
							break;
						}
					}
				};
				mLoginlTask = new LoginlTask(mContext, callback);
				mLoginlTask.execute(bean);
			}
		});

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		release();
	}

	private LoginlTask mLoginlTask = null;

	public void release() {
		username = null;
		password = null;
		remember = null;
		autologin = null;
		login = null;
		mLoginlTask.release();
	}

	private EditText username = null;
	private EditText password = null;
	private CheckBox remember = null;
	private CheckBox autologin = null;
	private ImageView login = null;

	private void init() {
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		remember = (CheckBox) findViewById(R.id.remember);
		autologin = (CheckBox) findViewById(R.id.autologin);
		login = (ImageView) findViewById(R.id.login);

	}
}